package com.fivetalks.controller.web;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fivetalks.domain.Post;
import com.fivetalks.service.PostService;

@Controller
public class PostController {
	@Autowired
	PostService postService;
	
	@RequestMapping(value="/post/list", method = RequestMethod.GET)
	public String list(
		Model model
	){
		List<Post> list = postService.getPostList();
		model.addAttribute("list",list);
		return "/post/list";
	}//list
	
	@RequestMapping(value="/post/writeform", method = RequestMethod.GET)
	public String writeform(
		Model model
	){
		return "/post/writeform";
	}//writeform
	
	@RequestMapping(value="/post/write", method = RequestMethod.POST)
	public String write(
			@RequestParam(value="name", required=true)String name,
			@RequestParam(value="passwd", required=true)String passwd,
			@RequestParam(value="content", required=true)String content,
			Model model
	){
		
		Post post = new Post();
		post.setName(name);
		post.setPasswd(passwd);
		post.setContent(content);
		postService.savePost(post); //저장
		return "redirect:/post/list"; // /post/list로 자동이동
	}//write
	
}
